/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */


package org.evswork.getwit;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TweetView extends LinearLayout {
	TextView UserText;
	TextView MsgText;
	ImageView ImgStatus;
	TweetObject data;
	
	static int ColorCache [][];
	
	public static int [][] getColorCache(Context ctxt) {
		SharedPreferences pref=ctxt.getSharedPreferences("uidata", Activity.MODE_PRIVATE);
		if (ColorCache == null) {
			ColorCache = 
			new int [][] {
				new int [] {
					pref.getInt("color_0u", 0xff606060),
					pref.getInt("color_0c", 0xff000000),
					pref.getInt("color_0b", 0xffFFDBBF)
				},
				new int [] {
					pref.getInt("color_1u", 0xff606060),
					pref.getInt("color_1c", 0xff000000),
					pref.getInt("color_1b", 0xffD6BFFF)
				},
				new int [] {
					pref.getInt("color_2u", 0xff606060),
					pref.getInt("color_2c", 0xff000000),
					pref.getInt("color_2b", 0xffBFF5FF)
				},
				new int [] {
					pref.getInt("color_3u", 0xff606060),
					pref.getInt("color_3c", 0xff000000),
					pref.getInt("color_3b", 0xffFFF4BF)
				}
			};
		}
		return ColorCache;
	}
	
	public static void refreshColorCache(Context ctxt) {
		SharedPreferences pref=ctxt.getSharedPreferences("uidata", Activity.MODE_PRIVATE);
		
		// make sure cache can be access
		getColorCache(ctxt);
		
		ColorCache[0][0] = pref.getInt("color_0u", 0xff606060);
		ColorCache[0][1] = pref.getInt("color_0c", 0xff000000);
		ColorCache[0][2] = pref.getInt("color_0b", 0xffFFDBBF);
		ColorCache[1][0] = pref.getInt("color_1u", 0xff606060);
		ColorCache[1][1] = pref.getInt("color_1c", 0xff000000);
		ColorCache[1][2] = pref.getInt("color_1b", 0xffD6BFFF);
		ColorCache[2][0] = pref.getInt("color_2u", 0xff606060);
		ColorCache[2][1] = pref.getInt("color_2c", 0xff000000);
		ColorCache[2][2] = pref.getInt("color_2b", 0xffBFF5FF);
		ColorCache[3][0] = pref.getInt("color_3u", 0xff606060);
		ColorCache[3][1] = pref.getInt("color_3c", 0xff000000);
		ColorCache[3][2] = pref.getInt("color_3b", 0xffFFF4BF);
	}
	
	public TweetView(Context context) {
		super(context);
		initView();
	}
	
	public TweetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView() {
		LayoutInflater.from(getContext()).inflate(R.layout.tweet, this,true);
		UserText = (TextView)this.findViewById(R.id.user_text);
		MsgText = (TextView)this.findViewById(R.id.tweet_text);
		ImgStatus = (ImageView)this.findViewById(R.id.img_status);
	}

	public void update(TweetObject data) {
		this.data = data;
		UserText.setText(data.user);
		MsgText.setText(data.content);
		
		if (data.recent)
		{
			ImgStatus.setVisibility(View.VISIBLE);
		} else {
			ImgStatus.setVisibility(GONE);
		}
		
		getColorCache(getContext());
		this.UserText.setTextColor(ColorCache[data.flag][0]);
		this.MsgText.setTextColor(ColorCache[data.flag][1]);
		this.setBackgroundColor(ColorCache[data.flag][2]);
	}
}
